package com.xyd.servlet;

import com.xyd.entity.News;
import com.xyd.service.NewsService;
import com.xyd.service.impl.NewsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 任梦强
 */
@WebServlet("/NewsUpdateServlet")
public class NewsUpdateServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String enIdStr=request.getParameter("enId");
        String enTitle=request.getParameter("Title");
        String enContent=request.getParameter("Content");

        long enId=Long.parseLong(enIdStr);
        NewsService newsService=new NewsServiceImpl();

        News news=new News(enId,enTitle,enContent);



        Boolean flag=newsService.updateNews(news);


        if (flag){
            request.getRequestDispatcher("NewsServlet?pageIndex=0").forward(request,response);
        }
        else {
            response.sendRedirect("index.html");
            System.out.println("修改错误！");
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
